package com.iwunu.gateway.service.impl;

import javax.annotation.Resource;

import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import com.iwunu.gateway.entity.CmdIssued;
import com.iwunu.gateway.service.CmdIssuedMongodbService;

@Component
public class CmdIssuedMongodbServiceImpl implements CmdIssuedMongodbService {
	 @Resource
	 private MongoTemplate mongoTemplate;
	 
	public CmdIssued getCmdIssuedByDeviceId(String clientId,Integer pitNum,Integer type) {
		Query query = new Query(Criteria.where("clientId").is(clientId).and("pitNum").is(pitNum).and("type").is(type));
		CmdIssued cmdIssued = mongoTemplate.findOne(query, CmdIssued.class);
		return cmdIssued;
	  }
	
	public void updateCmdIssuedStatus(CmdIssued cmdIssued) {
		Query query = new Query(Criteria.where("clientId").is(cmdIssued.getClientId()).and("pitNum").is(cmdIssued.getPitNum()).and("type").is(cmdIssued.getType()));
		Update update = Update.update("isValid", cmdIssued.isValid()).set("isSucc", cmdIssued.isSucc());
		mongoTemplate.findAndModify(query, update, CmdIssued.class);
	  }
	public void removeCmdIssuedsByDeviceId(String clientId,String openType) {
		Query query = new Query(Criteria.where("clientId").is(clientId).and("type").is(openType));
		 mongoTemplate.remove(query, CmdIssued.class);
	  }
	public void removeCmdIssuedsByDeviceId(CmdIssued cmdIssued) {
		Query query = new Query(Criteria.where("clientId").is(cmdIssued.getClientId()).and("type").is(cmdIssued.getType()).and("pitNum").is(cmdIssued.getPitNum()));
		 mongoTemplate.remove(query, CmdIssued.class);
	  }
}
